<?php
class JobsController extends AppController {

    public $name = 'Jobs';

    public function index(){
        $this->Job->recursive = 0;
        $this->set('jobs', $this->paginate());
    }

    public function view($id = null){
        if(!$id){
            $this->Session->setFlash(__('Invalid Job.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Job->recursive = 0;
        $this->set('job', $this->Job->read(null, $id));
    }

    public function viewByCompany($company_id){
        if(!$company_id){
            $this->Session->setFlash(__('Invalid Company', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->loadModel('Company');
        $jobs = $this->Job->find('all', array('conditions'=>array('company_id'=>$company_id)));
        $company = $this->Company->read(null, $company_id);
        $this->set(compact('jobs', 'company'));
    }

    public function add(){
        if(!empty($this->data)){
            $this->Job->create();
            if($this->Job->save($this->data)){
                $this->Session->setFlash(__('The Job has been saved', true));
                $this->redirect(array('action'=>'index'));
            }else{
                $this->Session->setFlash(__('The Job could not be saved. Please, try again.', true));
            }
        }
        $companies = $this->Job->Company->find('list');
        $this->set(compact('companies'));
    }

    public function edit($id = null){
        if(!$id && empty($this->data)){
            $this->Session->setFlash(__('Invalid Job', true));
            $this->redirect(array('action'=>'index'));
        }
        if(!empty($this->data)){
            if($this->Job->save($this->data)){
                $this->Session->setFlash(__('The Job has been saved', true));
                $this->redirect(array('action'=>'index'));
            }else{
                $this->Session->setFlash(__('The Job could not be saved. Please, try again.', true));
            }
        }
        if(empty($this->data)){
            $this->data = $this->Job->read(null, $id);
        }
        $companies = $this->Job->Company->find('list');
        $this->set(compact('companies'));
    }

    public function delete($id = null){
        if(!$id){
            $this->Response->json(false, __('Invalid id for Job', true));
        }
        if($this->Job->del($id)){
            $this->Response->json(true, __('Job deleted', true), $id);
        }
    }

    public function student(){
        //debug($this->params);
        if(!isset($this->params['named']['export'])){
            $this->params['named']['export'] = 0;

        }
        extract($this->params['named']);
        if(!$job){
            $this->Session->setFlash(__('Invalid job id', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Job->recursive = 1;
        $data = $this->Job->read(null, $job);
        if($export){
            $this->autoRender = false;
            $content = $this->generateCsv($data['Student']);
            $this->Response->csv($content);
        }else{
            $this->set('job', $data);
        }
    }
    /* Generate CSV string from student data */
    private function generateCsv($students){
        $content = '';
        foreach($students as $student){
            unset($student['StudentsJob']);
            unset($student['created']);
            unset($student['modified']);
            unset($student['students_job_count']);
            unset($student['student_minority_id']);
            unset($student['student_province_id']);
            unset($student['resume']);
            unset($student['active']);
            $content .= implode(";", $student);
            $content .= ";\n";
        }
        $keys = array_keys($student);
        $header = implode(";", $keys);
        $header .= ";\n";
        return $header . $content;
    }
    public function search(){}
}
?>